package sh.wn36.vehiclemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.controller.voe.DriverQuery;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.entity.Driver;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.service.DriverService;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.vo.driverInfo;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@CrossOrigin
@Api(value = "驾驶员信息")
@RestController
@RequestMapping("/driver")
public class DriverController {

    @ApiModelProperty("成员变量 DriverService")
    @Autowired
    private DriverService driverService;
    @Autowired
    private DictionaryService dictionaryService;

    @ApiModelProperty("分页查询驾驶员")
    @PostMapping("/ListDriver/{current}/{limit}")
    public Result pageListDriver(@ApiParam(value = "当前页码",name = "currentPage",required = true)@PathVariable("current")Integer current,
                                 @ApiParam(value = "每页显示记录数",name = "pageSize",required = true)@PathVariable("limit")Integer limit
                                 ){
        Page<driverInfo> page = new Page<>(current,limit);
        IPage<driverInfo> driverInfo1 = driverService.getAll2(page);
        long total = page.getTotal();
        List<driverInfo> driverInfo = driverInfo1.getRecords();

        return Result.OK().data("total",total).data("items",driverInfo);
    }


    @ApiModelProperty("通过条件分页查询驾驶员")
    @PostMapping("/pageListDriver/{current}/{limit}")
    public Result pageListDriver(@ApiParam(value = "当前页码",name = "currentPage",required = true)@PathVariable("current")Integer current,
                                 @ApiParam(value = "每页显示记录数",name = "pageSize",required = true)@PathVariable("limit")Integer limit,
                                 @ApiParam(value = "查询条件",name = "driverQuery",required = false)@RequestBody(required = false) DriverQuery driverQuery
                                 ){
        Page<driverInfo> page = new Page<>(current,limit);
        QueryWrapper<driverInfo> wrapper = new QueryWrapper<>();

        if(driverQuery != null){
            Integer select = driverQuery.getSelect();
            if(select != null && select== 1) {
                String name = driverQuery.getName();
                if (!StringUtils.isEmpty(name)) {
                    wrapper.like("name", name);
                }
                String phone = driverQuery.getPhone();
                if (!StringUtils.isEmpty(phone)) {
                    wrapper.like("phone", phone);
                }
            }else if(select != null &&select == 2){
                String name = driverQuery.getName();
                wrapper.like("phone",name);
            }
        }
        wrapper.orderByDesc("gmt_modified");
        IPage<driverInfo> driverInfo1 = driverService.getAll1(page, wrapper);
        long total = page.getTotal();
        List<driverInfo> driverInfo = driverInfo1.getRecords();
        return Result.OK().data("total",total).data("items",driverInfo);
    }

    @ApiModelProperty("保存驾驶员信息")
    @PostMapping("/saveDriver")
    public Result saveDriver(@RequestBody Driver driver){
        boolean result = driverService.save(driver);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiModelProperty("查出驾驶员信息")
    @GetMapping("/findDriverById/{id}")
    public Result findDriverById(@ApiParam(value = "驾驶员ID",required = true,name = "id")@PathVariable("id")Integer id){
        Driver driver = driverService.getById(id);
        return Result.OK().data("driver",driver);
    }

    @ApiModelProperty("修改驾驶员信息")
    @PostMapping("/updateDriver")
    public Result updateDriver(@ApiParam(value = "驾驶员信息",required = true,name = "id")@RequestBody Driver driver){

        boolean result = driverService.updateById(driver);
        if (result)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiModelProperty("删除驾驶员信息")
    @RequestMapping("/deleteDriver/{ids}")
    public Result deleteByIds(@PathVariable("ids") int[] ids){
        for (int i = 0; i < ids.length; i++) {
            int id = ids[i];
            driverService.removeById(id);
        }
        return Result.OK();


    }
    @ApiOperation("通过pId查询部门数据")
    @GetMapping("/getDeptById/{id}")
    public Result getListById(@ApiParam("部门id")@PathVariable("id")Integer id){
        List<Dictionary> list = dictionaryService.getListByPid(id);
        return Result.OK().data("dept",list);
    }




}

